import math
import os

from flask import Blueprint,jsonify,request

from models.model import db, Photo, Photo_album

user_bp = Blueprint("user_bp",__name__)


# 添加相册分类
@user_bp.route('/photoadd', methods=["POST"])
def photoadd():
    data = request.json
    name = data.get("name")
    phot = Photo_album(name=name)
    db.session.add(phot)
    db.session.commit()
    return jsonify({"code":200,"msg":"添加成功"})


# 查询相册分类数据
@user_bp.route("photget",methods=["GET"])
def photget():
    pho = Photo_album.query.all()
    pholist=[{"id":i.id,"name":i.name}for i in pho]
    return jsonify({"code":200,"pholist":pholist})


# 修改相册分类数据
@user_bp.route("/photupd",methods=["PUT"])
def photupd():
    data = request.json
    cid = data.get("cid")
    name = data.get("name")
    phupd = Photo_album.query.filter_by(id=cid).first()
    phupd.id = cid
    phupd.name = name
    db.session.commit()
    return jsonify({"code":200,"msg":"修改成功"})


# 删除相册分类数据
@user_bp.route("/photdel",methods=["DELETE"])
def photdel():
    cid = request.args.get("cid")
    Photo_album.query.filter_by(id=cid).delete()
    db.session.commit()
    return jsonify({"code":200,"msg":"删除成功"})


# 添加相册
@user_bp.route("/phadd",methods=["POST"])
def phadd():
    data = request.json
    name = data.get("name")
    url = data.get("url")
    padd = Photo(name=name,url=url)
    db.session.add(padd)
    db.session.commit()
    return jsonify({"code":200,"msg":"添加成功"})


@user_bp.route("/phgetone",methods=["GET"])
def phgetone():
    cid = request.args.get("cid")
    pone = Photo.query.filter_by(id=cid)
    ponelist = [{"id":i.id,"name":i.name,"url":i.url}for i in pone]
    return jsonify({"code":200,"ponelist":ponelist})


# 展示相册下所有相片
@user_bp.route('/phget',methods=["GET"])
def phget():
    cid = request.args.get("cid")
    pget = Photo.query.filter(Photo.photo_album_id == cid)
    pgetlist=[{"id":i.id,"name":i.name,"url":i.url}for i in pget]
    return jsonify({"code":200,"pgetlist":pgetlist})


# 修改相册
@user_bp.route("/phupd",methods=["PUT"])
def phupd():
    data = request.json
    cid = data.get("cid")
    name = data.get("name")
    url = data.get("url")
    photo_album_id = data.get("photo_album_id")
    pupd = Photo.query.filter_by(id=cid).first()
    pupd.id = cid
    pupd.name = name
    pupd.url = url
    pupd.photo_album_id = photo_album_id
    db.session.commit()
    return jsonify({"code":200,"msg":"修改成功"})


# 删除相册
@user_bp.route("phdel",methods=["DELETE"])
def phdel():
    cid = request.args.get("cid")
    pdel = Photo.query.filter_by(id=cid).delete()
    db.session.commit()
    return jsonify({"code":200,"msg":"删除成功"})


# 上传图片
@user_bp.route("phfigt",methods=["POST"])
def phfigt():
    file = request.args.get("file")
    path = os.path.join("static/upload"+file.filename)
    file.save(path)
    return jsonify({"code":200,"url":path})


# 分页
@user_bp.route("/phofen",methods=["GET"])
def phofen():
    page = int(request.args.get("page"))
    page_size = 2
    cid = request.args.get("cid")
    page_pho = Photo.query.filter(Photo.photo_album_id == cid).count()
    start = math.ceil((page-1)*page_size)
    page_fen = Photo.query.filter(Photo.photo_album_id == cid).offset(start).limit(page_size).all()
    page_list = [{"id":i.id,"name":i.name,"url":i.url}for i in page_fen]
    return jsonify({"code":200,"page_list":page_list,"page_pho":page_pho})







